//package 百度生成唯一ID_UidGenerator.conf;
//
//import com.baidu.fsg.uid.utils.DockerUtils;
//import com.baidu.fsg.uid.utils.NetUtils;
//import com.baidu.fsg.uid.worker.WorkerIdAssigner;
//import com.baidu.fsg.uid.worker.WorkerNodeType;
//import com.baidu.fsg.uid.worker.entity.WorkerNodeEntity;
//import org.apache.commons.lang.math.RandomUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//import 百度生成唯一ID_UidGenerator.dao.WorkerNodeMapper;
//
///**
// * Represents an implementation of {@link WorkerIdAssigner},
// * the worker id will be discarded after assigned to the UidGenerator
// *
// * @author yutianbao
// */
//@Component("disposableWorkerIdAssigner")
//public class DisposableWorkerIdAssigner implements WorkerIdAssigner {
//    private static final Logger LOGGER = LoggerFactory.getLogger(DisposableWorkerIdAssigner.class);
//
//    @Autowired
//    private WorkerNodeMapper workerNodeMapper;
//
//    /**
//     * Assign worker id base on database.<p>
//     * If there is host name & port in the environment, we considered that the node runs in Docker container<br>
//     * Otherwise, the node runs on an actual machine.
//     *
//     * @return assigned worker id
//     */
//    @Override
//    @Transactional
//    public long assignWorkerId() {
//        // build worker node entity
//        WorkerNodeEntity workerNodeEntity = buildWorkerNode();
//
//        // add worker node for new (ignore the same IP + PORT)
//        workerNodeMapper.addWorkerNode(workerNodeEntity);
//        LOGGER.info("Add worker node:" + workerNodeEntity);
//
//        return workerNodeEntity.getId();
//    }
//
//    /**
//     * Build worker node entity by IP and PORT
//     */
//    private WorkerNodeEntity buildWorkerNode() {
//        WorkerNodeEntity workerNodeEntity = new WorkerNodeEntity();
//        if (DockerUtils.isDocker()) {
//            workerNodeEntity.setType(WorkerNodeType.CONTAINER.value());
//            workerNodeEntity.setHostName(DockerUtils.getDockerHost());
//            workerNodeEntity.setPort(DockerUtils.getDockerPort());
//
//        } else {
//            workerNodeEntity.setType(WorkerNodeType.ACTUAL.value());
//            workerNodeEntity.setHostName(NetUtils.getLocalAddress());
//            workerNodeEntity.setPort(System.currentTimeMillis() + "-" + RandomUtils.nextInt(100000));
//        }
//
//        return workerNodeEntity;
//    }
//
//}